package com.xindaoapp.happypet.fragments;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.EmptyAdapter;
import com.xindaoapp.happypet.adapter.MyAttentionAdapter;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.Fans;
import com.xindaoapp.happypet.bean.Fans.Fan;
import com.xindaoapp.happypet.ui.BaseFragment;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 发帖-引用帖子-我的关注
 * 
 * @author xindaoapp
 * 
 */
public class MyAttentionFragment extends BaseFragment {

    /**
     * 
     * @param fid
     * @param isBack 是否点击之后返回
     * @return
     */
    public static Fragment newInstance(String fid, boolean isBack) {
        Fragment fragment = new MyAttentionFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY_GROUP_FID, fid);
        bundle.putBoolean(Constants.KEY_IS_CLICK_TO_BACK, isBack);
        fragment.setArguments(bundle);
        return fragment;
    }

    private PullToRefreshListView vPullToRefreshListView;
    private Button btn_search;
    private EditText et_search;
    private String searchText;
    private ProgressDialog progressDialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.layout_listview_with_divider, container, false);
        vPullToRefreshListView = (PullToRefreshListView) rootView.findViewById(R.id.listview);
        vPullToRefreshListView.setMode(Mode.PULL_FROM_START);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        View sreachHeader = View.inflate(getActivity(), R.layout.item_searchview, null);
        btn_search = (Button) sreachHeader.findViewById(R.id.iv_shop_search);
        et_search = (EditText) sreachHeader.findViewById(R.id.et_search);

        vPullToRefreshListView.getRefreshableView().addHeaderView(sreachHeader);

        btn_search.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                hideKeyBoard();
                searchText = et_search.getText().toString().trim();
                if (TextUtils.isEmpty(searchText)) {
                    Toast.makeText(getActivity(), "请输入关键字", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 查询我的好友
                searchMyAttention();

            }


        });

        vPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                et_search.getText().clear();
                loadDatas();
            }
        });



        vPullToRefreshListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fan info = (Fan) parent.getAdapter().getItem(position);
                if (getArguments().getBoolean(Constants.KEY_IS_CLICK_TO_BACK)) {
                    Intent data = new Intent();

                    data.putExtra(Constants.KEY_UID, info.followuid);
                    data.putExtra(Constants.KEY_FANS_USERNAME, info.fusername);
                    data.putExtra(Constants.KEY_FANS_USERFACE, info.userface);
                    getActivity().setResult(Activity.RESULT_OK, data);
                    getActivity().finish();
                } else {
                    Intent intent =
                            new Intent(getActivity(),
                                    com.xindaoapp.happypet.activity.MainActivity.class);
                    intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                            UserInfoFragment.class.getCanonicalName());
                    Bundle bundle = new Bundle();
                    bundle.putString(Constants.KEY_UID, info.followuid);
                    bundle.putString(Constants.KEY_USERNAME, info.fusername);
                    bundle.putString(Constants.KEY_USERFACE, info.userface);
                    intent.putExtra(Constants.KEY_BUNDLE, bundle);
                    getActivity().startActivity(intent);
                }
            }
        });
    }

    private void searchMyAttention() {
        progressDialog = ProgressDialog.show(mContext, null, "获取数据中...", true);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(true);
        getMoccaApi().searchMyAttention(searchText, new IRequest<Fans>() {
            @Override
            public void request(Fans t) {
                if (progressDialog != null) {
                    progressDialog.cancel();
                }
                if (t != null && t.array != null && t.array.size() > 0) {
                    vPullToRefreshListView.setAdapter(new MyAttentionAdapter(getActivity(),
                            t.array, Constants.PAGE_SIZE, R.layout.fans_list_item,
                            R.layout.item_loading, getArguments()
                                    .getString(Constants.KEY_GROUP_FID)));
                } else {
                    vPullToRefreshListView.setAdapter(new EmptyAdapter(getActivity()));
                }
            }
        });
    }

    @Override
    protected void loadDatas() {
        super.loadDatas();
        HappyPetApplication
                .get()
                .getMoccaApi()
                .getFollowList(UserState.getUserUid(), Constants.PAGE_FIRST, Constants.PAGE_SIZE,
                        getArguments().getString(Constants.KEY_GROUP_FID), new IRequest<Fans>() {

                            @Override
                            public void request(Fans t) {
                                if (null != t) {
                                    onDataArrived(true);
                                    if (t.array.size() == 0) {
                                        onDataArrivedEmpty();
                                    } else {
                                        vPullToRefreshListView.setAdapter(new MyAttentionAdapter(
                                                getActivity(), t.array, Constants.PAGE_SIZE,
                                                R.layout.fans_list_item, R.layout.item_loading,
                                                getArguments().getString(Constants.KEY_GROUP_FID)));
                                    }
                                } else {
                                    onDataArrived(false);
                                }
                                vPullToRefreshListView.onRefreshComplete();
                            }
                        });
    }

}
